import { request } from './request'

export interface WorkspaceItem {
  id: number
  name: string
  description?: string
  projectCount?: number
  memberCount?: number
  createdAt?: string
}

export interface WorkspaceListResponse {
  list?: WorkspaceItem[]
  total?: number
}

const extractList = (data: WorkspaceListResponse | WorkspaceItem[]) => {
  if (Array.isArray(data)) {
    return data
  }
  if (Array.isArray(data?.list)) {
    return data.list
  }
  return []
}

export async function getWorkspaceList(params = { pageNum: 1, pageSize: 100 }) {
  const data = await request<WorkspaceListResponse | WorkspaceItem[]>({
    url: '/lowcode3/api/workspace/list',
    data: params
  })
  return extractList(data)
}

export async function createWorkspace(payload: {
  name: string
  description?: string
}) {
  return request<WorkspaceItem>({
    url: '/lowcode3/api/workspace/add',
    data: payload
  })
}

export async function deleteWorkspace(workspaceId: number) {
  return request({
    url: '/lowcode3/api/workspace/delete',
    data: { id: workspaceId }
  })
}
